package com.coolwen.controller;

import com.coolwen.entity.User;
import com.coolwen.service.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpSession;

import org.springframework.web.bind.annotation.SessionAttributes;

import java.util.List;

/**
 * Created by user on 2016/5/4.
 */
@Controller
@RequestMapping("/login")
@SessionAttributes("loginUser")
public class LoginController {

    @Autowired
    UserService loginService;

    @RequestMapping(value = "/do_login", method = RequestMethod.GET)
    public String login(Model model) {
//        LoginUser loginUser=new LoginUser();
        model.addAttribute(new User());
        return "login/login";
    }

    @RequestMapping(value = "/do_login", method = RequestMethod.POST)
    public String login(@Validated User user, BindingResult br, HttpSession session) {

        if (br.hasErrors()) {
            //如果有错误直接跳转到add视图
            return "login/login";
        }
        String loginName = user.getUsername();
        String loginPassword = user.getPassword();

        if (loginService.selectUserByUserName(loginName) == null || !loginService.selectUserByUserName(loginName).getPassword().equals(loginPassword)) {
//            throw new UserException("用户名和密码错误!");
            return "/login/login_error";
        }

        session.setAttribute("loginUser", user);
        return "redirect:/collect/collect_info";
    }

    /**
     * 局部异常处理,仅仅只能处理这个控制器中的异常！需要处理全部需要在spring-mvc.xml中配置
     *
     *
     */
//    @ExceptionHandler(value = {UserException.class})
//    public String handlerException(UserException e,HttpServletRequest req){
//        req.setAttribute("e",e);
//        return "error";
    //   }
//    @RequestMapping(value = "/select", method = RequestMethod.GET)
//    public String select() {
//
//        return "/selectTest";
//    }
//    @RequestMapping(value = "/selected", method = RequestMethod.GET)
//    public String selected() {
//
//        return "/upload_error";
//    }
    /**
     * 用户注册模块
     */
    @RequestMapping(value = "/do_register", method = RequestMethod.GET)
    public String register(Model model) {
//        LoginUser loginUser=new LoginUser();
        model.addAttribute(new User());
        return "login/login";
    }
    @RequestMapping(value = "/do_register", method = RequestMethod.POST)
         public String register(@Validated User user,String username,String password,String nickname) {

        List<User> users = loginService.selectByName(username);

        System.out.println(users);


        if (users.isEmpty()){
            user.setNickname(nickname);
            user.setPassword(password);
            user.setUsername(username);

            loginService.insert(user);

            return "/login/register_successful";
        }else {

            return "/login/register_error";
        }
    }
}
